<?php
# http://projects:8081/cdapp-2kt-api/api/seat_hours_load_users.php

include_once '_portal.php';

const ORGID_NAMES = [
  ['orgid' => '12', 'name' => '管制一室'],
  ['orgid' => '13', 'name' => '管制二室'],
];

// const PARAM_NAME = "seatUsersLoadTime";
const PARAM_NAME_USERS = 'ATCOSUsers';
const PARAM_NAME_USERS_GROUPED = 'ATCOSUsersGrouped';

// $refresh_info = get_param(PARAM_NAME);

// added try function @20240120
$try = isset($_GET['try']) ? $_GET['try'] : 0;
if ($try > 0) {
  output(load_atcos_users());
  die();
}

# get user list
function load_atcos_users() {

  $users = [];
  $users_grouped = [];
  $success = true;
  foreach (ORGID_NAMES as $org) {
    $session = get_session();
    $list = get_users($session, $org['orgid']);
    if (!(isset($list) && is_array($list) && count($list) > 0)) {
      // $success = false;
      return ['success' => false, 'users' => $users, 'usersGrouped' => $users_grouped, 'orgID' => $org, 'list' => $list, 'session' => $session];
    }
    $children = [];
    foreach ($list as $item) {
      $users[] = [
        'name' => $item['text'],
        'number' => $item['value'],
        'office' => $org['name'],
      ];
      $children[] = [
        'label' => $item['text'],
        'value' => $item['value'],
      ];
    }
    $users_grouped[] = ['label' => $org['name'], 'value' => $org['orgid'], 'children' => $children];
  }
  if ($success) {
    save_param(PARAM_NAME_USERS, $users);
    save_param(PARAM_NAME_USERS_GROUPED, $users_grouped);
  }
  return ['status' => count($users) > 0 && $success, 'users' => $users, 'usersGrouped' => $users_grouped];
}

function get_session() {
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => 'http://172.22.107.24/WebReport/ReportServer?reportlet=%25E6%2596%25B0%25E7%2589%2588_%25E7%25AE%25A1%25E5%2588%25B6%25E6%25B4%25A5%25E8%25B4%25B4%25E4%25B8%25AA%25E4%25BA%25BA%25E6%258A%25A5%25E8%25A1%25A8.cpt&op=view&userId=19&orgId=4',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
  ));
  $response = curl_exec($curl);
  curl_close($curl);
  $pattern = "/FR\.SessionMgr\.register\('(\d*)', contentPane\);/";
  preg_match($pattern, $response, $matches);
  $session = (int)$matches[1];
  return $session;
}

function get_users($session_id, $org_id) {
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => "http://172.22.107.24/WebReport/ReportServer?op=widget&widgetname=userId2&sessionID={$session_id}",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => "dependence=%7B%22ORGID%22%3A%5B5b%5D{$org_id}%5B5d%5D%7D&startIndex=0&limitIndex=500&reload=true",
    // dependence=%7B%22ORGID%22%3A{$org_id}%7D&startIndex=0&limitIndex=500&reload=true
    CURLOPT_HTTPHEADER => array(
      'Content-Type: application/x-www-form-urlencoded',
    ),
  ));
  $response = curl_exec($curl);
  curl_close($curl);
  $response = iconv("gbk", "utf-8//ignore", $response);
  $response = json_parse($response);
  return $response;
}